﻿/*
 * LINQ扫盲大会
 */
#region Select 和 SelectMany

//Select 和 SelectMany
//var users = new List<User>()
//{
//    new User(){Name = "张三",Roles = new List<Role>(){new Role(){Name = "管理员"}}},
//    new User(){Name = "李四",Roles = new List<Role>(){new Role(){Name = "管理员"},new Role(){Name = "审核员"}}},
//    new User(){Name = "王武",Roles = new List<Role>(){new Role(){Name = "普通会员"}}},
//    new User(){Name = "马柳",Roles = new List<Role>(){new Role(){Name = "管理员"},new Role(){Name = "审核员"},new Role(){Name = "普通会员"}}},
//};

//// 1.Select 映射加数据处理  从源数据挑选成为新的数据源
//users.Select(x => x.Name).ToList().ForEach(Console.WriteLine);
//users.Select((x, index) => new { x.Name, index }).ToList().ForEach(x => Console.WriteLine($"{x.index}:{x}"));


//// 2.SelectMany 从源数据挑选成为新的数据源
//users.SelectMany(x => x.Name).ToList().ForEach(Console.WriteLine);
//users.SelectMany(x => x.Roles, ((user, role) => new { UserName = user.Name, RoleName = role.Name })).ToList()
//    .ForEach(x => Console.WriteLine($"{x.UserName}:{x.RoleName}"));

//class User
//{
//    public string Name { get; set; }
//    public IEnumerable<Role> Roles { get; set; } = new List<Role>();
//}

//class Role
//{
//    public string Name { get; set; }
//} 
#endregion


#region Aggregate

//// Aggregate 聚合,从一个集合中第一个和第二个运算后,得到一个结果,再带入下一次运算中
////1+2+3+4+...
////1+2=3
////3+3=6
////6+4=10
////...
//var range = Enumerable.Range(1, 100);
//var res = range.Aggregate((first, second) => first + second);
//Console.WriteLine(res); //5050

//var res2 = range.Aggregate(10000, (first, second) => first + second);
//Console.WriteLine(res2); //15050;  第一个参数表示种子  从几起步开始运算

//var dicts = new Dictionary<string, object>()
//{
//    ["Name"] = "张三",
//    ["Age"] = 18,
//    ["Code"] = "123456"
//};

//var res3 = dicts.Select(x => $"{x.Key}={x.Value}").Aggregate((prev, next) => $"{prev}&{next}");
//Console.WriteLine(res3);

#endregion


#region LINQ分页

var dataSource = Enumerable.Range(1, 1000);
int pageIndex = 1;
int pageSize = 10;

do
{
    Console.Write("请输入页码:");
    pageIndex = int.Parse(Console.ReadLine());
    if (pageIndex < 0)
    {
        break;
    }

    var pageSource = dataSource.Skip((pageSize - 1) * pageSize).Take(pageSize); //每页数据
    var totalCount = dataSource.Count(); // 总数
    var totalPages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1; // 页数
} while (pageIndex > 0);

#endregion
